package com.zch.zhongchou.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zch.zhongchou.dao.ProjectsMapper;
import com.zch.zhongchou.dao.UserMapper;
import com.zch.zhongchou.pojo.Projects;
import com.zch.zhongchou.pojo.User;
import com.zch.zhongchou.service.UserService;
import com.zch.zhongchou.util.PageSize;
@Service("us")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper dao;
	@Autowired
	private ProjectsMapper pm;
	@Override
	public Map<String,Object> login(String strPhoneOrEmail,String password){
		HashMap<String, Object> map = new HashMap<String,Object>();
		User user = dao.getUserByKey(strPhoneOrEmail);
		if(user != null){
			if(user.getUsPassword().equals(password)){
				map.put("user1", user);
				map.put("isLogin", true);
				map.put("msg", "登录成功");
			}else{
				map.put("isLogin", false);
				map.put("msg", "密码错误");
			}
		}else{
			map.put("isLogin", false);
			map.put("msg", "此用户不存在");
		}
		return map;
	}
	
	@Override
	public int add(User user) {
		// TODO Auto-generated method stub
		return dao.insert(user);
	}

	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		return dao.deleteByPrimaryKey(id);
	}

	@Override
	public int update(User user) {
		// TODO Auto-generated method stub
		return dao.updateByPrimaryKey(user);
	}

	@Override
	public User getUserById(int id) {
		// TODO Auto-generated method stub
		return dao.selectByPrimaryKey(id);
	}

	@Override
	public List<User> getAllUserByPage(String name, String phone, int sex, int pageIndex) {
		Map<String, Object> map = new HashMap<>();
		map.put("name", name);
		map.put("phone", phone);
		map.put("sex", sex);
		int firstRow = (pageIndex - 1)*PageSize.PAGE_SIZE;
		int lastRow = PageSize.PAGE_SIZE;
		map.put("firstRow", firstRow);
		map.put("lastRow", lastRow);
		List<User> list = dao.getAllUserByPage(map);
		return list;
	}

	@Override
	public int getTotalCount(String name, String phone, int sex) {
		Map<String, Object> map = new HashMap<>();
		map.put("name", name);
		map.put("phone", phone);
		map.put("sex", sex);
		int counts = dao.getTotalCount(map);
		return counts;
	}

	@Override
	public int getTotalPage(String name, String phone, int sex) {
		Map<String, Object> map = new HashMap<>();
		map.put("name", name);
		map.put("phone", phone);
		map.put("sex", sex);
		int counts = dao.getTotalCount(map);
		int totalPages = counts % PageSize.PAGE_SIZE == 0 ? 
				counts/PageSize.PAGE_SIZE:counts/PageSize.PAGE_SIZE+1;
		return totalPages;
	}

	@Override
	public User getUserByName(String strPhoneOrEmail) {
		return dao.getUserByKey(strPhoneOrEmail);
	}
	@Override
	public List<Projects> getProjectsByusId(Integer usId){
		return pm.getProjectsByusId(usId);
	}

}
